﻿#region Header

/*
Remote views based on linq
By

Pascal Hauser 
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

and

Raphael Gfeller
Dipl. Ing. in Informatik, Hochschule für Technik Rapperswil, 2006
Master Thesis, Hochschule für Technik Rapperswil, 2008-2010

*/

#endregion

#region Usings

using System;
using System.Diagnostics;
using System.Linq;

#endregion

namespace Sebarf.Diagnostics.Interfaces {
	/// <summary>
	/// interface for a 
	/// </summary>
	internal class SimpleCheckerService : ICheckerService {
		public const int DEBUG = 0;
		public const int ERROR = 1000;
		public const int WARNING = 100;

		#region Public Methods

		/// <summary>
		/// checks if the given argument is not null
		/// </summary>
		/// <param name="name"></param>
		/// <param name="argsName"></param>
		/// <exception cref="ArgumentNullException"/>
		public void ArgumentNotNull(object name, string argsName) {
			if (name is String) {
				if (string.IsNullOrEmpty(name as string)) {
					Logger.WriteError("ArgumentNotNull: " + argsName);
					throw new ArgumentNullException(argsName);
				}
			}
			if (name == null) {
				Logger.WriteDebug(string.Concat(new StackTrace().GetFrames().Select(f => f + "\n")));
				Logger.WriteError("ArgumentNotNull: " + argsName);
				throw new ArgumentNullException(argsName);
			}
			return;
		}

		/// <summary>
		/// checks if the given condition is false, if so a expection with the given message is generated
		/// </summary>
		/// <param name="condition"></param>
		/// <param name="message"></param>
		public void AssertException(bool condition, string message) {
			if (!condition) {
				Logger.WriteError(message);
				throw new Exception(message);
			}
		}

		/// <summary>
		/// checks if the given condition is false, if so a warning message will be generated
		/// </summary>
		/// <param name="condition"></param>
		/// <param name="message"></param>
		public void AssertWarning(bool condition, string message) {
			if (!condition) {
				Logger.WriteWarning(message);
			}
		}

		/// <summary>
		/// checks if the given condition is false, if so a debug message will be generated
		/// </summary>
		/// <param name="condition"></param>
		/// <param name="message"></param>
		public void AssertDebug(bool condition, string message) {
			if (!condition) {
				Logger.WriteDebug(message);
			}
		}

		#endregion

		#region ICheckerService Members

		public void Assert(int priority, bool condition, string message) {
			switch (priority) {
				case WARNING:
					AssertWarning(condition, message);
					break;
				case ERROR:
					AssertException(condition, message);
					break;
				case DEBUG:
					AssertDebug(condition, message);
					break;
				default:
					AssertException(condition, message);
					break;
			}
		}

		#endregion
	}
}